/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"
	"log"
	"net"
	"os"

	"gitee.com/spark-community-works-collections/shenmo-water-pipe/utils"
	"github.com/spf13/cobra"
)

// resetCmd represents the reset command
var resetCmd = &cobra.Command{
	Use:   "reset",
	Short: "重置服务器",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		r, _ := cmd.Flags().GetString("remote")
		if r == REMOTE_DEFAULT {
			log.Println("Use Default Remote")
			p, _ := cmd.Flags().GetInt("port")
			s, _ := cmd.Flags().GetString("host")
			conn, err := net.Dial("tcp", fmt.Sprintf("%v:%v", s, p))
			if err != nil {
				log.Fatal(err)
			}
			conn.Write([]byte(ACCESS_POLICY))
			conn.Write([]byte(ACCESS_RESET_POLICY))
			utils.TransferData(conn, os.Stdout)
		}
	},
}

func init() {
	rootCmd.AddCommand(resetCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// resetCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// resetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
	resetCmd.Flags().IntP("port", "p", REMOTE_PUSH, "目标端口")
	resetCmd.Flags().StringP("host", "s", REMOTE_HOST, "目标主机")
	resetCmd.Flags().StringP("remote", "r", REMOTE_DEFAULT, "目标远程配置")
}
